import { generateDrivers } from './data'

// 司机数据模拟
const drivers = generateDrivers();
// 获取司机列表
export default defineEventHandler(async (event) => {
  try {
    const query = event.context.params || {};
    const page = Number.parseInt(query.page as string) || 1;
    const pageSize = Number.parseInt(query.pageSize as string) || 20;
    const fullName = query.fullName as string;
    const phoneNumber = query.phoneNumber as string;

    // 筛选数据
    let filteredDrivers = drivers;

    if (fullName) {
      filteredDrivers = filteredDrivers.filter((driver) =>
        driver.fullName.includes(fullName),
      );
    }

    if (phoneNumber) {
      filteredDrivers = filteredDrivers.filter((driver) =>
        driver.phoneNumber.includes(phoneNumber),
      );
    }

    // 分页处理
    const total = filteredDrivers.length;
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const data = filteredDrivers.slice(startIndex, endIndex);

    return {
      code: 200,
      message: 'success',
      data: {
        list: data,
        total,
        page,
        pageSize,
      },
    };
  } catch (error) {
    console.error('获取司机列表失败:', error);
    return {
      code: 500,
      message: '获取司机列表失败',
      data: null,
    };
  }
});
